﻿;
/*
表单提交
*/
function validateCallback(form, callback, confirmMsg) {
    var $form = $(form);

    if (!$form.valid()) {
        return false;
    }

    var _submitFn = function () {
        $.ajax({
            type: form.method || 'POST',
            url: $form.attr("action"),
            data: $form.serializeArray(),
            dataType: "json",
            cache: false,
            success: callback || BootStrapUI_Core.ajaxDone,
            error: BootStrapUI_Core.ajaxError
        });
    }

    if (confirmMsg) {
        parent.layer.confirm(confirmMsg, { title: '提示', icon: 3 }, _submitFn);
    } else {
        _submitFn();
    }

    return false;
}

/*
    ajax提交
*/
function postAjax(url, data, confirmMsg, callback) {
    var _submitFn = function () {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: "json",
            data:data,
            cache: false,
            success: callback || navTabAjaxDone,
            error: BootStrapUI_Core.ajaxError
        });
    }

    if (confirmMsg) {
        parent.layer.confirm(confirmMsg, { title: '提示', icon: 3 }, _submitFn);
    } else {
        _submitFn();
    }

    return false;
}

//Tab Ajax-form提交回调事件
function navTabAjaxDone(json) {
    BootStrapUI_Core.ajaxDone(json);
    if (json.statusCode == BootStrapUI_Core.statusCode.ok) {
        if (json.redirectUrl) {
            window.location.href = json.redirectUrl;
        }
    }
}

function dialogAjaxDone(json) {
    BootStrapUI_Core.ajaxDone(json);
    if (json.statusCode == BootStrapUI_Core.statusCode.ok) {
        if (json.redirectUrl) {
            window.location.href = json.redirectUrl;
        } else {
            var $dlg = $('#dlg');
            var $flexigridData = $('#flexigridData');
            var $flexigridTree = $('#flexigridTree');
            if ($dlg.length > 0) $dlg.dialog('close'); 	// close the dialog
            if ($flexigridData.length > 0) $flexigridData.datagrid('reload'); // reload the user data
            if ($flexigridTree.length > 0) $flexigridTree.treegrid('reload');
        }
    }
}
